#!/usr/bin/python
# -*-coding:utf-8-*-

# from zg02_factor_lib.base.factors_library_base import NewFactorLib
from zbc_factor_lib.base.factors_library_base import NewRQFactorLib as NewFactorLib

from zg_data_process.zg_data_concat import DataConcat
from zg_data_process.zg_data_process import DataProcess

data_reader = NewFactorLib(db='validation')

data_concat_api = DataConcat()
data_process_api = DataProcess()

# TODO - 读取数据
start_date = '2021-01-01'
end_date = '2021-01-31'

factor_name = 'profitability_roe_ttm'

factor_data = data_reader.read_factor_table(factor_name,
                                            filter_list=["date >= '%s' and date <= '%s'" %
                                                         (start_date, end_date)])

# TODO - 拼接数据
label_factor_data = data_concat_api.label_factor_data(
    df=factor_data,
    start_date=None,
    end_date=None,
    cache=False,
    cache_filename=None,
    add_size_factor=True,
    refresh=True,
    verbose=True,
    copy=True
)


# TODO - 特征处理
## 缺失值填补
filtered_label_factor_data = data_process_api.filled_with_market_stats_value(df=label_factor_data, columns=[factor_name], q=0.5)

## 数据过滤
# st过滤
filtered_label_factor_data = data_process_api.st_filteration(df=filtered_label_factor_data, drop_label=False)

# 停牌股过滤
filtered_label_factor_data = data_process_api.paused_stock_filtration(df=filtered_label_factor_data, drop_label=False)

# 一字板过滤
filtered_label_factor_data = data_process_api.oneline_stock_filtration(df=filtered_label_factor_data, drop_label=False)

# 新股&次新股过滤
filtered_label_factor_data = data_process_api.new_subnew_stock_filtration(df=filtered_label_factor_data, drop_label=False)

## 数据去重
filtered_label_factor_data = data_process_api.duplicates_merge(df=filtered_label_factor_data, columns=[factor_name], rule='mean', copy=True)

## 离异值（极值）处理
filtered_label_factor_data = data_process_api.cs_mad_outlier_process(df=filtered_label_factor_data, columns=[factor_name], n=3*4826, copy=True)

## 归一化和中性化处理
filtered_label_factor_data = data_process_api.cs_sw1_ind_normalization_process(df=filtered_label_factor_data, columns=[factor_name])



